package com.zit.java.service.sysuser;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zit.java.controller.BaseController;
import com.zit.java.dao.SysUserMapper;
import com.zit.java.pojo.SysRoleUser;
import com.zit.java.pojo.SysUser;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class UserSerivceImpl extends BaseController implements UserSerivce {

    @Autowired
    private SysUserMapper dao;


    @Override
    public SysUser queryUser(int id) {
        SysUser sysUser = dao.selectByPrimaryKey(id);
        return sysUser;
    }


    @Override
    public int updataData(SysUser sysUser) {

        int rst = dao.updateByPrimaryKeySelective(sysUser);

        return rst;
    }

    @Override
    public int delUser(int id) {
        int rst = dao.deleteByPrimaryKey(id);

        return rst;
    }

    @Override
    public int updateUser(int id, SysUser sysUser) {
        sysUser.setId(id);
        int rst = dao.updateByPrimaryKeySelective(sysUser);
        return rst;
    }


    @Override
    public TableData<SysUser> queryUserByCondition(String name, String loginName, int page, int limit) {
        PageHelper.startPage(page, limit);
        Page<SysUser> pageList = dao.queryUserByCondition(name, loginName);
        TableData<SysUser> tableData = new TableData<>();
        tableData.setCode(0);
        tableData.setMsg("ok");
        tableData.setCount(pageList.size());
        tableData.setData(pageList.getResult());
        return tableData;
    }

    @Override
    public List<Map<String, Object>> queryAllRoles(String id) {
        return dao.queryAllRolesByUser(id);
    }

    @Override
    public int updateUserRole(String[] roleIds, String id, HttpServletRequest request) {
        //删除用户已有的角色权限
        dao.deleteByuserId(id);
        Integer num = 1;
                new Thread(()->{
                    for (String roleId : roleIds) {
                        SysRoleUser sysRoleUser = new SysRoleUser();
                        sysRoleUser.setUserId(Integer.valueOf(id));
                        sysRoleUser.setRoleId(Integer.parseInt(roleId));
                        dao.insertSrSelective(sysRoleUser);
                    }
                }).start();

        return num;
    }

    /**
     * 删除用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public int deleteUser(SysUser sysUser) {
        int rst = dao.deleteByPrimaryKey(sysUser.getId());
        return rst;
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public int saveUser(SysUser sysUser) {
        int rst = dao.insertSelective(sysUser);
        return rst;
    }

    @Override
    public int batchDelUser(List<String> ids) {
        Integer s = 0;
        for (String id : ids) {
            s += dao.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return s;
    }

    @Override
    public int updateUserDate(SysUser sysUser) {
        int i = dao.updateByPrimaryKeySelective(sysUser);
        return i;
    }

    @Override
    public TableData<SysUser> queryUserByName(String name) {
        SysUser tb = dao.selectByUserName(name);
        TableData<SysUser> tableData = new TableData<>();
        tableData.setCode(0);
        tableData.setMsg("ok");
        tableData.setCount(1L);
        tableData.setData(tb);
        return tableData;
    }
}
